import { Application } from 'egg';
import {
  navRule,
  indexRule,
  createRule,
  updateRule,
  destroyRule
 } from '../validate/goods/category';

/**
 * 用户组管理的路由
 */
export default (app: Application) => {
  const { auth } = app.middleware;
  const { jwt } = app;

  return {
    'category.list.all': {
      method: 'GET',
      path: '/api/goods/category/all',
      options: {
        tags: ['goods/category/all'],
        description: '商品分类所有列表',
        handler: app.controller.goods.category.listAll,
        middlewares: [jwt, auth('goods.category.all')],
      },
    },
    'category.list': {
      method: 'GET',
      path: '/api/goods/category/list',
      options: {
        tags: ['goods/category/list'],
        description: '商品分类列表',
        handler: app.controller.goods.category.list,
        middlewares: [jwt, auth('goods.category.list')],
        validate: indexRule,
      },
    },

    'category.nav': {
      method: 'GET',
      path: '/api/goods/category/nav',
      options: {
        tags: ['goods/category/list'],
        description: '商品列表分类选择',
        handler: app.controller.goods.category.nav,
        // middlewares: [jwt, auth()],  // 小程序接口
        validate: navRule,
      },
    },
    'category.create': {
      method: 'POST',
      path: '/api/goods/category',
      options: {
        tags: ['goods/category/create'],
        description: '创建商品分类',
        handler: app.controller.goods.category.create,
        middlewares: [jwt, auth('goods.category.create')],
        validate: createRule,
      },
    },
    'category.update': {
      method: 'PUT',
      path: '/api/goods/category/:id',
      options: {
        tags: ['goods/category/update'],
        description: '更新商品分类',
        handler: app.controller.goods.category.update,
        middlewares: [jwt, auth('goods.category.update')],
        validate: updateRule,
      },
    },
    'category.delete': {
      method: 'DELETE',
      path: '/api/goods/category/:id',
      options: {
        tags: ['goods/category/delete'],
        description: '删除商品分类',
        handler: app.controller.goods.category.destroy,
        middlewares: [jwt, auth('goods.category.destroy')],
        validate: destroyRule,
      },
    }
  };
};
